package com.atguigu.rabbitmq.springbootmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO 延迟队列 交换机和队列的定义
 * @author zhangbanglong
 * @date 2024/1/13 21:00
 * @version 1.0
 */
@Configuration
public class DelayedQueueConfig {

    public static  final  String QUEUE_NM="delayed.queue";

    public static  final  String EXCHANGE_NM="delayed.exchange";

    public static  final String ROUTING_KEY="delayed.routingkey";

    @Bean
    public Queue delayedQueue(){
        return new Queue(QUEUE_NM);
    }

    //定义一个延迟交换机
    @Bean
    public CustomExchange delayedExchange(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(EXCHANGE_NM,"x-delayed-message",true,false,args);
    }

    @Bean
    public Binding bindingDelayQueue(@Qualifier("delayedQueue")Queue queue, @Qualifier("delayedExchange")CustomExchange delayedExchange){
        return BindingBuilder.bind(queue).to(delayedExchange).with(ROUTING_KEY).noargs();

    }
}
